#include "asr.h"

#include "log.h"

namespace chat {
BaiDuYunASRClient::BaiDuYunASRClient(const std::string& app_id,
                                     const std::string& app_key,
                                     const std::string& secret_key)
    : client_(std::make_shared<aip::Speech>(app_id, app_key, secret_key)) {}

const std::string BaiDuYunASRClient::asr(const std::string& pcm,
                                         std::string* err_message) {
    Json::Value result = client_->recognize(pcm, "pcm", 16000, aip::null);

    if (result[std::string("err_no")].asInt() != 0) {
        LOG_ERROR("语音识别失败: {}", result[std::string("err_msg")].asString());
        *err_message = result[std::string("err_msg")].asString();

        return "";
    }

    *err_message = "success";
    return result[std::string("result")][0].asString();
}

bool BaiDuYunASRClient::getPcmContent(const std::string& pcm_path,
                                      std::string* file_content) {
    if (!aip::get_file_content("16k.pcm", file_content)) {
        LOG_DEBUG("获取pcm数据成功");
        return true;
    } else {
        LOG_ERROR("获取pcm数据失败");
        return false;
    }
}

}  // namespace chat
